import {  base, toast, promptBox} from 'nc-lightapp-front';
import {hrAjax as ajax} from 'src/hrpub/common/utils/utils';
export default class Action {

    constructor(props) {
        this.props = props;
        this.buttonFlag = true
        this.pubProps = props.props
        if(this.props.use){
            this.props.use.editTable('trans_item_list');
        }
    }
    didMount = () => {
        window.location.hash = `#ifr?page=2019`;
        let callback = (json, status, inlt) => {
            if (status) {
                this.props.setState({json, inlt},()=>{
                })//存json和inlt到页面state中并刷新页面
            }
        }
        let height = document.documentElement.clientHeight - 100;
        this.props.props.MultiInit.getMultiLang({moduleId: 'gx6008', domainName: 'hrtrn', callback})
            this.getTemplateData()
            this.initTableDate()
            this.props.setState({pageHeight:height})
    }

    didUpdate = () => {
        
    }
    getTemplateData = () => {
        const {createUIDom, meta, button} = this.pubProps;
        createUIDom(this.pubProps.appConfig, (data = {}) => {
            meta.setMeta(data.template || {});
            button.setButtons(data.button || {});
            button.setButtonVisible({ //初始化按钮
                setup: this.buttonFlag, 
                refresh: this.buttonFlag,  
                print:this.buttonFlag,
                save:!this.buttonFlag,
                cancel:!this.buttonFlag,
                all_sel:!this.buttonFlag,
                all_unsel:!this.buttonFlag
            })
            button.setButtonDisabled({
                setup: this.buttonFlag 
            })
            
            
        });
    }

    /**
     *修改更新区域编码
     */
    updateAreacode = (code,callback) =>  {
        this.props.setState({
            areacode:code
        },()=>{
            if(!callback) {
                return
            }
            callback()
        })
    }

    /**
     * 修改按钮显隐状态
     */
    updateButtonFlag = (code,callback) => {
        this.props.setState({
            buttonFlag:code
        },()=> {
            if(!callback) {
                return
            }
            callback()
        })
    }

    /**
     * 初始化表格数据
     */
    initTableDate = () => {
        ajax({
            url:'/nccloud/hrtrn/transItem/TransItemQueryAction.do',
            success:(res)=> {
                if(res.success === true) {
                    this.initDateView(res)
                }
            }
        })
    }
    
    initDateView = (res) => {
        if(res.data){
            //获取区域编码
            this.updateAreacode(res.data.trans_item_list.areacode)  
            // res.data.trans_item_list.pageInfo = {pageIndex:0,pageSize:100}
           
            // 先生成表格以获取表格rowid
            this.pubProps.editTable.setTableData(this.props.state.areacode, res.data.trans_item_list)
            if(res.data.trans_item_list.pageInfo){
                delete res.data.trans_item_list.pageInfo
            }
            // 用有行id的数据来覆盖原数据
            res.data.trans_item_list.rows = this.pubProps.editTable.getAllRows(this.props.state.areacode)

            // 提取数据
            this.props.setState({
                initDate:res.data.trans_item_list
            })
            // set表格数据
            this.setTableDate(res.data.trans_item_list)
        } else {
            this.props.setState({
                dateFlag:false
            })
        }
    }

    /**
     * 返回选中数据
     */
    setTableDate = (date) => {
        let data = {
            stType:this.props.state.orgValue.refpk
        }
        ajax({
            url:'/nccloud/hrtrn/transItem/TransItemSetQueryAction.do',
            data:data,
            success:(res) => {
                if(res.success === true) {
                    //获取对象长度
                    if(res.data) {
                        let count = Object.keys(res.data).length.toString()
                        this.props.setState({
                            selectCount:count
                        })
                        // 挂载数据
                        this.setTool(date,res)
                       
                    } else {
                        let count = '0'
                        this.props.setState({
                            selectCount:count
                        })
                    }
                    this.pubProps.editTable.setTableData(this.props.state.areacode, date)
                    
                   
                }
            }
        })
    }
    cancelData = () => {
        this.props.setState({
            defaultDate:{}
        })
        this.initTableDate()
    }

    /**
     * 刷新数据重新挂载
     */
    refreshData = () => {
        this.setTableDate(this.props.state.initDate)
    }
    /**
     * 挂载数据公共方法
     * @param {object} res 选中数据
     * @param {object} date 模板基础数据
     */
    setTool = (date,res) => {
          //设置数据
        date.rows = date.rows.map((item) => {
            for (let key in res.data) {
                //设置返回对象的key如果与item的infoset值相等就挂载选中状态
                if(key === item.values.pk_infoset_item.value) {
                    item.values.isdefault.value = res.data[key].isdefault
                    item.values.isedit.value = res.data[key].isedit
                    item.values.ismustnotnull.value = res.data[key].ismustnotnull
                    item.values.isshow.value = res.data[key].isshow
                    this.checkboxVisible(this.props.state.areacode,item.rowid,['isedit','ismustnotnull','isdefault'],true)
                }
            }
            return item
        })
    }
    

    /**
     * 多选框是否可编辑公用方法
     * @param areacode 区域编码 
     * @param rowid 行id
     * @param {arry} arry 是否可编辑数组key值
     * @param flag 显隐状态
     */
    checkboxVisible = (areacode,rowid,arry,flag) => {
        this.pubProps.editTable.setEditableRowKeyByRowId(areacode, rowid, arry, flag)
    }

    /**
     * 禁用默认选中状态
     */
    disableDefaultData = () => {
         this.props.state.initDate.rows.forEach((item) => {
            for (let key in this.props.state.defaultDate.data) {
                //设置返回对象的key如果与item的infoset值相等就挂载选中状态
                if(key === item.values.pk_infoset_item.value) {
                    this.checkboxVisible(this.props.state.areacode,item.rowid,['isedit','ismustnotnull','isshow'],false)
                    this.checkboxVisible(this.props.state.areacode,item.rowid,['isdefault'],true)
                } 
                
            }
        }) 
    }

    /**
     * 全消
     * @param {arry} arr 取消勾选key值数组
     */
    editAllUnsel = (arr,flag) => {
        //全部消除
        this.props.state.initDate.rows.forEach((item,index) => {
            arr.forEach((j) => {
                this.pubProps.editTable.setValByKeyAndIndex(this.props.state.areacode, index, j,{value:flag})
            }) 
            
        });
        //挂载默认数据
        this.setTool(this.props.state.initDate,this.props.state.defaultDate)
        
        //锁定全部数据
        this.props.state.initDate.rows.forEach((item) => {
            this.checkboxVisible(this.props.state.areacode,item.rowid,['isedit','ismustnotnull','isdefault'],false)
        })

         // 锁定默认数据
         this.disableDefaultData()
            
    }
    /**
     * 设置表格
     */
    setUpTable = () => {
        let count = this.props.state.selectCount
        let data = {
            selectedNum:count,
            stType:this.props.state.orgValue.refpk
        }
        ajax({
            url:'/nccloud/hrtrn/transItem/TransItemSetupAction.do',
            data:data,
            success:(res) => {
                if(res.success === true) {
                    this.props.setState({
                        defaultDate:res
                    },()=>{
                        this.disableDefaultData()
                    })
                    if(count !== '0') {
                        return
                    }
                    this.setTool(this.props.state.initDate,res)
                    this.disableDefaultData()
                    
                }
            }
        })
    }

    /**
     * 保存
     */
    save = () => {
        
        let data = {
            stType:this.props.state.orgValue.refpk
        }

        data.trans_item_list = {}
        data.trans_item_list.areacode = this.props.state.areacode
        data.trans_item_list.rows = this.pubProps.editTable.getAllRows(this.props.state.areacode).filter((item)=>{
            // 过滤表格选中数据 
            return item.values.isshow.value === true
        })
        ajax({
            url:'/nccloud/hrtrn/transItem/TransItemSetSaveAction.do',
            data:data,
            success:(res) => {
                if(res.success === true) {
                    // 设置表格为禁止编辑状态
                    this.pubProps.editTable.setStatus(this.props.state.areacode,'browse')

                    //调取按钮状态函数
                    this.props.refs.buttonTool.action.header.buttonTool(this.pubProps,true)
                    toast({color:"success",title:this.props.state.json['gx6008-000030']}) //保存成功
                    
                }
            }
        })
    }

    /**
     * 判断当前行可点多选框状态
     */
    clickHandle = (props,arearcode,item,index) => {
        // console.log(a,b,c,d)
    }

    /**
     * 多选框点击后的回调事件
     * 判断当前行各个多选框是否可点击状态
     * @param areacode 区域编码 
     * @param key 属性名
     * @param value 属性值
     * @param {object} chackvalue 多选框选中前后状态值
     * @param {object} item 当前行的数据值
     */
    afterEventFn = (props,areacode,key,value,chackvalue,index,item) => {
        if(key === 'isshow' && value === true) {
            //checkbox可用 设置某行某属性的可编辑性
            this.checkboxVisible(areacode,item.rowid, ['isedit','ismustnotnull','isdefault'], true)

            //改变某个字段的value值
            this.editValue(props,areacode,item,['isedit','isdefault'],{value:true})
        } else if(key === 'isshow' && value === false){   
            //checkbox禁用
            this.checkboxVisible(areacode,item.rowid, ['isedit','ismustnotnull','isdefault'], false)

            //改变某个字段的value值
            this.editValue(props,areacode,item,['isedit','ismustnotnull','isdefault'],{value:false})
        } else if(key === 'isedit' && value === true) {
            //改变某个字段的value值
            this.editValue(props,areacode,item,['isdefault'],{value:true})

            //checkbox禁用
            this.checkboxVisible(areacode,item.rowid,['ismustnotnull','isdefault'], true)
        } else if(key==='isedit' && value === false) {
            //改变某个字段的value值
            this.editValue(props,areacode,item,['ismustnotnull'],{value:false})

            //checkbox禁用
            this.checkboxVisible(areacode,item.rowid,['ismustnotnull','isdefault'], false)

        }
      
    }

    /**
     * 根据选中状态修改多选框value值
     * @param {object} item 选中行数据
     * @param {arr} arry 要改变的key值数组
     * @param {object} value 改变的value
     */
    editValue = (props,areacode,item,arry,value) => {
        arry.forEach(element => {
            props.editTable.setValByKeyAndRowId(areacode,item.rowid,element,value)
        });
    }
    /**
     * 人力资源组织
     */
    handleChangePeople = (value) => {
        let setDisabled = this.props.props.button.setButtonDisabled
        this.props.setState({
            orgValue:value
        },()=>{
            this.initTableDate()
        })
        if(!value.refpk) {
            setDisabled({
                setup: true
            })
        } else {
            setDisabled({
                setup: false
            })
        }
    }

}